<?php 
require 'Model.php';
class IndexModel extends Model{
	function __construct(){
		parent::__construct();
		$this->mod = 'cartoons';
	}





function reg()
{
	
if($_POST["vid"]==0){//如果是首页登录
$u=$_POST["username"];
$p=$_POST["pssword"];
$sql="select * from users where uname='$u' and password='$p'";
//执行 sql
$rs=$this->conn->query($sql);
//$num=mysql_num_rows($rs[0]);
if($rs[0])
{
session_start();
$_SESSION["user"]=$u;
header("location:index.php");
}
else
header("location:index.php?re=1&msg=登录失败，请重新登录");
}else{
//如果是内容页登录
$vid=$_POST["vid"];
$u=$_POST["username"];
$p=$_POST["password"];
$sql="select * from users where uname='$u' and password='$p'";
//执行 sql
$rs=$this->conn->query($sql);
//$num=mysql_num_rows($rs[0]);
if($rs[0]>0)
{ session_start();
$_SESSION["user"]=$u;
header("location:show.php?vid=$vid");
}
//如果登录失败
else{
header("location:show.php?re=1&msg=登录失败，请重新登录&vid=$vid");
}
}
}










/*function reg()
	{
  $username = $_POST["username"];
$pwd = $_POST["password"];
  $sql = "select * from users where uname='".$username."' and password='".$pwd."'"; 
  $data = $this->conn->query($sql);
  return $data;
	}*/









		function redirect($url, $msg)
{	  
   echo $msg.'<a href="'.$url.'">如果没有跳转，请点这里跳转</a>';
   header("refresh:3;url=$url"); 
}

	function login()
	{
  $username = $_POST["username"];
$pwd = $_POST["password"];
  $sql = "select * from admin where adminname='".$username."' and password='".$pwd."'"; 
  $data = $this->conn->query($sql);
  return $data;
	}
	function adds($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
  				}
			  else{
			  	$pic='';
			  }

			  $one = array(
					'uname' => trim($data['username']),  
					'password' => $data['password'],
					'gender' => $data['gender'],
					'birthdate' => $data['birthdate'],
					'hobby' => implode($data['hobby'], ','),
					'degree' => $data['degree'],
					'intro' => $data['intro'],
					'pic'=>$pic
			  	);
			  $keys = implode(',',array_keys($one));
			  $values = implode("','", array_values($one));
			  $sql="insert into users ({$keys}) VALUES ('{$values}')";
			  //执行SQL语句
				$result = $this->conn->query($sql);
					return $result;
			  
		}
	function getList(){
		$sql_ct="select * from cartoontype";
		$ct_list = $this->conn->query($sql_ct);
		
		$c_list=array();
		foreach($ct_list as $one){
		    $sql="select * from {$this->mod} where tid=".$one['tid']." order by id  limit 0,6 ";
		   
		    $clist=array();
		    $clist['typename']=$one['typename'];
		    $clist['tid']=$one['tid'];
		    $clist['list']=$this->conn->query($sql);
		    $c_list[]=$clist;
		}
		    return $c_list;
	}
	function getCartoonType(){
		$sql_ct="select * from cartoontype";
		$data = $this->conn->query($sql_ct);
		
			return $data;
}


/*
 $id=$_GET['id'];
   $sql_vt='select * from videotype';
   $vt_list=fetch_array($sql_vt);

   $v_list=array();
   foreach ($vt_list as $one)
   {
       $vlist=array();
       $vlist['tid']=$one['tid'];
       $vlist['typename']=$one['typename'];
       $v_list[]=$vlist;
   }
   $sql='select * from videos where vid='.$id.'';
   $show=fetch_one($sql);*/















	function getCartoonByHint(){
	 $sql_hint="select * from {$this->mod} order by hint desc limit 0,4";
	 $data = $this->conn->query($sql_hint);
			return $data;
	}
	/*function getCartoonByDowntimes(){
		$sql_downtimes="select * from {$this->mod} order by downtimes desc limit 0,4";
		$data = $this->conn->query($sql_downtimes);
			return $data;

	}*/
	function getCartoonById($id){
		$sql="select * from {$this->mod} where id=".$id;
	    $data = $this->conn->query($sql);
		return $data;

	}
	function getCartoonTypeByTid($id){
		$sql="select typename from cartoontype where tid={$id}";
	    $data = $this->conn->query($sql);
		return $data;
		var_dump($data);

	}
	function getCartoonTypeByid($id){
		$id=$_GET['id'];
		$sql="select tid from {$this->mod} where id={$id}";
	    $data = $this->conn->query($sql);
	    /*$sql1= "select typename from cartoontype where tid=".$data['tid'];
	    
	    $data1 = $this->conn->query($sql1);
	   */
		return $data;

	}
	/*function getCartoonByid($id){
		$sql="select * from cartoons where id={$id}";
	    $data = $this->conn->query($sql);
	   
		return $data;
	}*/

	function search($key){
	$sql = "select * from {$this->mod} where name like '%{$key}%'";
	$data = $this->conn->query($sql);
	
		return $data;
}


}




 ?>